﻿using InventoryIssue.API.Write.Application.Command.Stocktaking;
using InventoryIssue.Domain;
using InventoryIssue.ErrorCode;
using InventoryIssue.Infrastructrue.Implement;
using InventoryIssue.Infrastructrue.Interface;
using MediatR;

namespace InventoryIssue.API.Write.Application.Handler.Stocktaking
{
    public class DelStocktakingHandler : IRequestHandler<DelStocktakingCommand, APIResult<int>>
    {
        private readonly DatabaseContext db;
        private readonly ILogger<DelStocktakingHandler> logger;

        public DelStocktakingHandler(DatabaseContext db,ILogger<DelStocktakingHandler> logger)
        {
            this.db = db;
            this.logger = logger;
        }
        public async Task<APIResult<int>> Handle(DelStocktakingCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = db.SqlSugar.Queryable<StocktakingModel>().Where(x => x.Id == request.Id).First();
            res.IsDel = true;
            int r = await db.SqlSugar.Updateable(res).ExecuteCommandAsync();
            if (r > 0)
            {
                result.Message = "删除成功";
                result.Data = r;
                logger.Log(LogLevel.Information, "盘点删除成功");
                result.Code = APIEnum.Ok;
            }
            else
            {
                result.Message = "删除失败";
                result.Data = r;
                result.Code = APIEnum.Fail;
            }
            return await Task.FromResult(result);
        }
    }
}
